package com.ttdevs.ttrss.ui.right;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ttdevs.ttrss.R;
import com.ttdevs.ttrss.app.AppInit;
import com.ttdevs.ttrss.db.IDBManager;

public class GroupFragment extends Fragment implements OnClickListener {

	private AddGroupDialog dialog;
	private ImageView ivAddGroup;

	public GroupFragment() {
		super();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		System.out.println("onCreateView");

		View view = inflater.inflate(R.layout.fragment_group, container, false);
		ivAddGroup = (ImageView) view.findViewById(R.id.ivAddGroup);
		ivAddGroup.setOnClickListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		dialog = new AddGroupDialog(getActivity(), R.style.customDialogTheme);
		dialog.setCancelable(false);

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivAddGroup:
			dialog.show();
			break;

		default:
			break;
		}

	}

	class AddGroupDialog extends Dialog implements OnClickListener {

		private IDBManager dbm;
		private Context context;
		private TextView tvAlert;
		private Button btCancel, btSubmit;
		private EditText etGroupName, etOrderKey;

		public AddGroupDialog(Context context) {
			super(context);
			this.context = context;
			this.dbm = AppInit.getDBManager();
		}

		public AddGroupDialog(Context context, int theme) {
			super(context, theme);
			this.context = context;
			this.dbm = AppInit.getDBManager();
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			// this.setView(R.layout.dialog_city_select);// 可能会有边框
			this.setContentView(R.layout.dialog_add_group);

			tvAlert = (TextView) findViewById(R.id.tvAlert);
			etGroupName = (EditText) findViewById(R.id.etGroupName);
			etOrderKey = (EditText) findViewById(R.id.etOrderKey);
			btCancel = (Button) findViewById(R.id.btCancel);
			btSubmit = (Button) findViewById(R.id.btSubmit);
			btCancel.setOnClickListener(this);
			btSubmit.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btSubmit:
				if("".equals(etGroupName.getText().toString())){
					tvAlert.setText("添加失败，请检查分组是否为空");
					break;
				}
				if (dbm.createGroup(etGroupName.getText().toString(), etOrderKey.getText().toString())) {
					clearForm();
					cancel();
				} else {
					tvAlert.setText("添加失败，请检查分组是否已经存在");
				}
				break;
			case R.id.btCancel:
				clearForm();
				cancel();
				break;

			default:
				break;
			}
		}
		
		private void clearForm(){
			etGroupName.setText("");
			etOrderKey.setText("");
			tvAlert.setText("");
		}
	}
}